package cn.ekuma.epos.qkorder.link;

import javax.swing.JPanel;
import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.util.SwingUtil;
import com.openbravo.bean.Product;
import com.openbravo.bean.SharedProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.panels.link.AbstractProductSelectTablePanel;
import com.openbravo.pos.panels.model.ProductTableModel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class JProductShardPanel extends AbstractProductSelectTablePanel<Product>{

	public JProductShardPanel(I_DataLogicSales dlSales) {
		super(dlSales, new ProductTableModel());
		loadDate();
	}

	private void loadDate() {
        try {
			tableModel.resert(dlSales.listProductByShared());
			 SwingUtil.PreferredJXTableColumn(jXTable1);
		} catch (BasicException e) {
			new MessageInf(e).show(this);
			tableModel.resert(null);
		}
	}

	@Override
	public void baseBeanChanged(IKeyed productID, Product o) {
	}

	
	
	@Override
	public String getProductId(Product obj) {
		return obj.getID();
	}

	@Override
	public JPanel getToolBar() {
		JPanel toolbar=new JPanel();
		FlowLayout flowLayout = (FlowLayout) toolbar.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		
		JButton btnNewButton = new JButton();
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadDate();
			}
		});
		btnNewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/reload.png"))); // NOI18N
		btnNewButton.setFocusable(false);
		btnNewButton.setFocusPainted(false);
		toolbar.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("");
		btnNewButton_1.setIcon(new ImageIcon(JProductShardPanel.class.getResource("/com/openbravo/images/editdelete.png")));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			 clearShardProducts();
			}
		});
		btnNewButton_1.setFocusable(false);
		btnNewButton_1.setFocusPainted(false);
		toolbar.add(btnNewButton_1);
		return toolbar;
	}

	protected void clearShardProducts() {
		try {
			 dlSales.clear(SharedProduct.class);
			tableModel.resert(null);
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}

	@Override
	public void baseBeanChanged(Product o) {
	}

	
	
}
